package com.hex.liyue.storemodule.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.fragment.BaseFragment;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.T;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.storemodule.activity.StoreDetailsActivity;
import com.hex.liyue.storemodule.adapter.AdapterStore;
import com.hex.liyue.storemodule.bean.FragmentStoreBean;
import com.hex.liyue.storemodule.presenter.FragmentStorePresenter;
import com.hex.liyue.storemodule.presenter.IFragmentStoreView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * Created by WJ on 2016/6/12.
 */
public class FragmentStore extends BaseFragment implements AdapterStore.OnItemClickListener, IFragmentStoreView, Serializable, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.rv_store)
    YRecycleview rvStore;

    private FragmentStorePresenter fragmentStorePresenter;
    private List<FragmentStoreBean.DataBean> list = new ArrayList<>();
    private AdapterStore adapterStore;
    private boolean sortPrice = true;//价格从高到低排
    private int page = 1, page_num = 10;

    @Override
    public void initdata(Bundle arguments) {
        layoutLoading();
        fragmentStorePresenter.getStoreList(page, page_num, sortPrice);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_store;
    }

    @Override
    protected void bindPresenter() {
        fragmentStorePresenter = new FragmentStorePresenter(this);
    }

    @Override
    protected void initToolbar() {
        tvToobarTitle.setText("商城");
        tvToobarRight.setVisibility(View.VISIBLE);
        if (sortPrice) {
            tvToobarRight.setText("价格从高到低");
        } else {
            tvToobarRight.setText("价格从低到高");
        }
    }


    @Override
    public void onItemClickListener(int position) {
        FragmentStoreBean.DataBean dataBean = list.get(position);
        Intent intent = new Intent(getActivity(), StoreDetailsActivity.class);
        intent.putExtra("position",position);
        intent.putExtra("dataBean", dataBean);
        startActivity(intent);
    }

    @OnClick({R.id.tv_toobar_right,R.id.ll_indiana,R.id.ll_trading})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_toobar_right://按价格排序
                if (sortPrice) {
                    sortPrice = false;
                } else {
                    sortPrice = true;
                }
                initToolbar();//更新标题栏信息
                fragmentStorePresenter.getStoreList(page, page_num, sortPrice);
                break;
            case R.id.ll_indiana://一元夺宝
                T.show(getActivity(),"此功能暂未开放",0);
                break;
            case R.id.ll_trading://二手交易 旧物拍卖
                T.show(getActivity(),"此功能暂未开放",0);
                break;
        }
    }

    @Override
    public void setAdapter(final FragmentStoreBean fragmentStoreBean) {
        if (page == 1) {
            list.clear();
        }
        if (fragmentStoreBean != null) {
            if (fragmentStoreBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (FragmentStoreBean.DataBean dataBean : fragmentStoreBean.data) {
                list.add(dataBean);
            }
        }
        if (adapterStore == null) {
            adapterStore = new AdapterStore();
            adapterStore.setList(list);
            adapterStore.setOnItemClickListener(this);
            rvStore.setLayoutManager(new LinearLayoutManager(getActivity()));
            rvStore.setRefreshAndLoadMoreListener(this);
            rvStore.setAdapter(adapterStore);
        }
        adapterStore.setList(list);
        adapterStore.notifyDataSetChanged();
        rvStore.reSetStatus();
        layoutContent();
        L.d("WJ", page + "=size=" + fragmentStoreBean.data.size());
    }

    @Override
    public void finishActivity() {

    }


    @Override
    public void showLod(String msg) {
        showLoad(getActivity(), msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(getActivity() , msg);
    }

    @Override
    public void onRefresh() {
        page = 1;
        fragmentStorePresenter.getStoreList(page, page_num, sortPrice);
    }

    @Override
    public void onLoadMore() {
        page++;
        fragmentStorePresenter.getStoreList(page, page_num, sortPrice);
    }
}
